package com.bckj.freshnow.order.trade.ons.listener;

import com.alibaba.cola.statemachine.StateMachineFactory;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.bckj.fastboot.core.jackson.JsonUtils;
import com.bckj.fastboot.mq.ons.OnsListener;
import com.bckj.freshnow.common.constant.PaymentMQConstants;
import com.bckj.freshnow.order.fsm.enums.OrderStateMachineIdEnum;
import com.bckj.freshnow.order.trade.fsm.TradeBillContext;
import com.bckj.freshnow.order.trade.fsm.enums.TradeBillStatementEventEnum;
import com.bckj.freshnow.order.trade.fsm.enums.TradeBillStatementStatusEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@OnsListener(topic = PaymentMQConstants.TOPIC, subExpression = PaymentMQConstants.TAG_COMPLETED)
public class PayCompletedListener implements MessageListener {

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        String json = new String(message.getBody());
        log.info("支付完成 j={}", json);

        //触发账单流水状态机
        TradeBillContext billContext = new TradeBillContext();
        //关联账单流水号
//        billContext.setBillStatementNo();
        StateMachineFactory.get(OrderStateMachineIdEnum.TRADE_BILL_STATEMENT.getMachineId())
                .fireEvent(TradeBillStatementStatusEnum.PENDING_PAYMENT, TradeBillStatementEventEnum.PAY_CALLBACK_SUCCESS, billContext);
        return Action.CommitMessage;
    }
}
